package com.eleadmin.store.biz.impl;

import com.eleadmin.damon.utils.constant.UserConstant;
import com.eleadmin.damon.utils.results.BaseDao;
import com.eleadmin.damon.utils.results.PageResult2;

import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.common.system.entity.User;
import com.eleadmin.store.biz.EntryBillSonBiz;
import com.eleadmin.store.entity.EntryBillSon;
import com.eleadmin.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2024-02-14 15:00:12
 */
@Repository
public class EntryBillSonBizImpl extends BaseDao implements EntryBillSonBiz {

    @Override
    public JsonResult list(PageResult2<T> page, EntryBillSon entryBillSon, User loginUser) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<EntryBillSon> rm = new BeanPropertyRowMapper<EntryBillSon>(EntryBillSon.class);
        StringBuffer sql = new StringBuffer("select e.id,e.entryBillId,e.organization_id");
        sql.append(",e.warehouseName,e.productName,e.specification,e.lengths");
        sql.append(",e.negative,e.num,e.liWeight,e.liTotalWeight");
        sql.append(",e.actualWeight,e.price,e.totalPrice");
        sql.append(",e.model,e.locality,e.create_time,e.create_user");
        sql.append(",e.update_time,e.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName,suser2.nickname updateName ");
        sql.append(" from we_entry_bill_son e ");
        sql.append(" inner join sys_user suser on suser.user_id=e.create_user ");
        sql.append(" left join sys_user suser2 on suser2.user_id=e.update_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=e.organization_id and sogzn.deleted=0 ");
        sql.append(" where e.deleted=0 and e.entryBillId=:entryBillId ");
        map.put("entryBillId", entryBillSon.getEntryBillId() + "");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and e.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(entryBillSon)) {
//            if (Utils.isNotEmpty(entryBillSon.getProductname())) {
//                sql.append(" and (instr(e.productName,:productName)>0 or instr(e.specification,:productName)>0 )");
//                map.put("productName", entryBillSon.getProductname());
//            }
            if (Utils.isNotEmpty(entryBillSon.getSort()) && Utils.isNotEmpty(entryBillSon.getOrder())) {
                sql.append(" order by e." + entryBillSon.getSort() + " " + entryBillSon.getOrder());
            } else {
                sql.append(" order by e.create_time asc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<EntryBillSon> list(EntryBillSon entryBillSon, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        RowMapper<EntryBillSon> rm = new BeanPropertyRowMapper<EntryBillSon>(EntryBillSon.class);
        StringBuffer sql = new StringBuffer("select e.id,e.entryBillId,e.organization_id");
        sql.append(",e.warehouseName,e.productName,e.specification,e.lengths");
        sql.append(",e.negative,e.num,e.liWeight,e.liTotalWeight");
        sql.append(",e.actualWeight,e.price,e.totalPrice");
        sql.append(",e.model,e.locality,e.create_time,e.create_user");
        sql.append(",e.update_time,e.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from we_entry_bill_son e ");
        sql.append(" inner join sys_user suser on suser.user_id=e.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=e.organization_id and sogzn.deleted=0 ");
        sql.append(" where e.deleted=0 and e.entryBillId=:entryBillId ");
        map.put("entryBillId", entryBillSon.getEntryBillId() + "");
        if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
            sql.append(" and e.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if (Utils.isNotEmpty(entryBillSon)) {
//            if (Utils.isNotEmpty(entryBillSon.getProductname())) {
//                sql.append(" and (instr(e.productName,:productName)>0 or instr(e.specification,:productName)>0 )");
//                map.put("productName", entryBillSon.getProductname());
//            }
            if (Utils.isNotEmpty(entryBillSon.getSort()) && Utils.isNotEmpty(entryBillSon.getOrder())) {
                sql.append(" order by e." + entryBillSon.getSort() + " " + entryBillSon.getOrder());
            } else {
                sql.append(" order by e.create_time asc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(), map, rm);
    }

    @Override
    public Double calcTotalPrice(EntryBillSon entryBillSon, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select IFNULL(sum(e.totalPrice),0) totalPrice ");
        sql.append(" from we_entry_bill_son e ");
        sql.append(" where e.deleted=0 and e.entryBillId=:entryBillId");
        map.put("entryBillId", entryBillSon.getEntryBillId()+"");
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, Double.class);
    }

    @Override
    public EntryBillSon getById(String id) {
        RowMapper<EntryBillSon> rm = new BeanPropertyRowMapper<EntryBillSon>(EntryBillSon.class);
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select e.id,e.entryBillId,e.organization_id");
        sql.append(",e.warehouseName,e.productName,e.specification,e.lengths");
        sql.append(",e.negative,e.num,e.liWeight,e.liTotalWeight");
        sql.append(",e.actualWeight,e.price,e.totalPrice");
        sql.append(",e.model,e.locality,e.create_time,e.create_user");
        sql.append(",e.update_time,e.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nickname createName ");
        sql.append(" from we_entry_bill_son e ");
        sql.append(" inner join sys_user suser on suser.user_id=e.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=e.organization_id and sogzn.deleted=0 ");
        sql.append(" where e.deleted=0 and e.id=:id ");
        map.put("id", id);
        getMapParameter(sql, map);
        try {
            return jdbcTemplate.queryForObject(sql.toString(), map, rm);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public boolean save(EntryBillSon entryBillSon, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("insert into we_entry_bill_son (");
        sql.append("entryBillId,organization_id,warehouseName,productName");
        sql.append(",specification,lengths,negative,num");
        sql.append(",liWeight,liTotalWeight,actualWeight");
        sql.append(",price,totalPrice,model,locality");
        sql.append(",create_user,create_time) values ( ");
        sql.append(":entryBillId,:organization_id,:warehouseName,:productName");
        sql.append(",:specification,:lengths,:negative,:num");
        sql.append(",:liWeight,:liTotalWeight,:actualWeight");
        sql.append(",:price,:totalPrice,:model,:locality");
        sql.append(",:create_user,now())");
        map.put("entryBillId", entryBillSon.getEntryBillId() == null ? null : entryBillSon.getEntryBillId() + "");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("warehouseName", entryBillSon.getWarehousename());
        map.put("productName", entryBillSon.getProductname());
        map.put("specification", entryBillSon.getSpecification());
        map.put("lengths", entryBillSon.getLengths());
        map.put("negative", entryBillSon.getNegative());
        map.put("num", entryBillSon.getNum()+"");
        map.put("liWeight", entryBillSon.getLiweight()+"");
        map.put("liTotalWeight", entryBillSon.getLitotalweight()+"");
        map.put("actualWeight", entryBillSon.getActualweight()+"");
        map.put("price", entryBillSon.getPrice()+"");
        map.put("totalPrice", entryBillSon.getTotalprice()+"");
        map.put("model", entryBillSon.getModel());
        map.put("locality", entryBillSon.getLocality());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(EntryBillSon entryBillSon, User loginUser) {
        Map<String, String> map = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("update we_entry_bill_son set ");
        sql.append("entryBillId=:entryBillId,warehouseName=:warehouseName,productName=:productName ");
        sql.append(",specification=:specification,lengths=:lengths,negative=:negative ");
        sql.append(",num=:num,liWeight=:liWeight,liTotalWeight=:liTotalWeight ");
        sql.append(",actualWeight=:actualWeight,price=:price ");
        sql.append(",totalPrice=:totalPrice,model=:model,locality=:locality ");
        sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("entryBillId", entryBillSon.getEntryBillId() == null ? null : entryBillSon.getEntryBillId() + "");
        map.put("warehouseName", entryBillSon.getWarehousename());
        map.put("productName", entryBillSon.getProductname());
        map.put("specification", entryBillSon.getSpecification());
        map.put("lengths", entryBillSon.getLengths());
        map.put("negative", entryBillSon.getNegative());
        map.put("num", entryBillSon.getNum()+"");
        map.put("liWeight", entryBillSon.getLiweight()+"");
        map.put("liTotalWeight", entryBillSon.getLitotalweight()+"");
        map.put("actualWeight", entryBillSon.getActualweight()+"");
        map.put("price", entryBillSon.getPrice()+"");
        map.put("totalPrice", entryBillSon.getTotalprice()+"");
        map.put("model", entryBillSon.getModel());
        map.put("locality", entryBillSon.getLocality());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", entryBillSon.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(String id, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if (id.length() > 0) {
            for (String id1 : id.split(",")) {
                map = new HashMap<String, String>();
                if (id1 == null || id1.equals("")) {
                    break;
                }
                StringBuffer sql = new StringBuffer("update we_entry_bill_son set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", id1);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(), map);
            }
        }
        return count > 0;
    }
}
